package org.xilin.service

import org.xilin.*
import javax.servlet.http.HttpSession
import org.springframework.web.context.request.RequestContextHolder

class SemesterService {

    static scope = "session"

    def grailsApplication

    static transactional = false

    void afterPropertiesSet()
    {
        //Class domainClass = grailsApplication.getClassLoader().loadClass(domainObjName, true)
    }

    private HttpSession getSession() {
        return RequestContextHolder.currentRequestAttributes().getSession()
    }
    
    def getCurrentSemester() {
        def today = new Date()
        def semester = Semester.findByEndDayGreaterThanAndStartDatLessThan(today, today)
    }

    def getActiveSemesters() {
        def semesters = Semester.findAllByActive('Y', [sort:"startDate", order:"asc"])
        if (getSession().activeSemester == null) {
            getSession().activeSemester = semesters[0]
        }
        return semesters
    }
    
    def getActiveSemester() {
        getSession().activeSemester
    }

    def setActiveSemester(semester) {
        getSession().activeSemester = semester
    }

    def getListBySemester(domainObj) {
        def list = domainObj.findAllBySemester(session.activeSemester)
        return list
    }
}
